package org.javaymw.springboot.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/28 11:23
 * @Description: 异步任务类
 */
@Component
public class AsyncTask {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取左侧菜单的一级缓存类
     * @return
     */
    protected Cache getLeftMenuTreeCacheName() {
        return cacheManager.getCache("LeftMenuTreeCache");
    }

    /**
     * 开启异步支持
     * @param ids
     */
    @Async
    public void deleteLeftCache(List<Long> ids) {
        if (!ids.isEmpty()) {
            ids.forEach(id -> {
                // 删除一级缓存 当前用户的菜单缓存
                getLeftMenuTreeCacheName().evict(id);
                // 删除二级缓存 当前用户的菜单缓存
                redisTemplate.opsForHash().delete(id, id);
            });
        }
    }
}
